import React, { useRef, useState } from 'react';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import { Button, Drawer, message, Popconfirm } from 'antd';
import { useRequest } from 'umi';
import AuditConfigForm from './AuditConfigForm'; // 引入表单组件
import { fetchAuditConfigList, deleteAuditConfig } from '@/services/ant-design-pro/audit'; // 假设你已经定义了这些服务
import { useModel } from '@umijs/max';
import { useAccess } from 'umi';
const AuditConfigList: React.FC = () => {
  const access = useAccess();
  const actionRef = useRef();
  const [drawerVisible, setDrawerVisible] = useState(false);
  const [currentRow, setCurrentRow] = useState(null);
  const [isEditMode, setIsEditMode] = useState(false);
  const { user } = useModel('userModel');
  const { role } = useModel('roleModel');
  const [activeKey, setActiveKey] = useState<string>('2');
  const { run: deleteRun } = useRequest(deleteAuditConfig, {
    manual: true,
    onSuccess: () => {
      message.success('删除成功');
      actionRef.current?.reload();
    },
    onError: () => {
      message.error('删除失败');
    },
  });

  const columns = [
    { title: '流程名称', dataIndex: 'flowName' },
    // { title: '条件', dataIndex: 'conditions',search:false , valueEnum:{
    //   'greaterThan': '大于',
    //   'lessThan': '小于',
    //   equal: '等于',
    // }},
    // { title: '状态', dataIndex: 'prOnoff', valueEnum: { 0: '停用', 1: '启用' } ,search:false },
    { title: '折扣', dataIndex: 'price' ,hidden: activeKey === '3' ? true : false },
    // { title: '价格比较', dataIndex: 'priceCompare',search:false,  
    // valueEnum: {
    //     greaterThan: '大于',
    //     lessThan: '小于',
    //     equal: '等于',
    //   }  },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a key="edit" onClick={() => handleEdit(record)}>编辑</a>,
        access.canAccessMenu({menuCode:'00001201'}) &&  <Popconfirm key="delete" title="确定删除该审批配置吗？" onConfirm={() => handleDelete(record.id)}>
          <a>删除</a>
        </Popconfirm>,
      ],
    },
  ];

  const handleEdit = (record) => {
    setCurrentRow(record);
    setIsEditMode(true);
    setDrawerVisible(true);
  };

  const handleDelete = (id) => {
    deleteRun(id);
  };

  const handleDrawerClose = () => {
    setDrawerVisible(false);
    setCurrentRow(null);
    setIsEditMode(false);
  };

  return (
    <PageContainer header={{ title: '' }}>
      <ProTable
        actionRef={actionRef}
        rowKey="id"
        request={(params)=>{
         return fetchAuditConfigList({...params,flowType:+activeKey})
        }}
        search={false}
        columns={columns}
        toolBarRender={() => [ activeKey !== '3' &&
          <Button key="add" type="primary" onClick={() => setDrawerVisible(true)}>
            添加审批配置
          </Button>,
        ]}
        toolbar={{
          menu: {
            type: 'tab',
            activeKey: activeKey,
            items: [
              { key: "2", label: <span>修改金额</span> },
              { key: "3", label: <span>申请退款</span> },
            ],
            onChange: (key) => {
              setActiveKey(key as string);
              actionRef.current?.reloadAndRest();
            },
          },
        }}
       
      />
      <Drawer
        title={isEditMode ? '编辑审批配置' : '添加审批配置'}
        width={600}
        open={drawerVisible}
        onClose={handleDrawerClose}
      >
        <AuditConfigForm
          initialValues={currentRow}
          isEditMode={isEditMode}
          onClose={handleDrawerClose}
          user={user}
          role={role}
          onSuccess={() => actionRef.current?.reload()}
        />
      </Drawer>
    </PageContainer>
  );
};

export default AuditConfigList;